﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

using Infrastructure.Core.IOC;
using Infrastructure.Dispatcher;
using Infrastructure.Utility;
using Infrastructure.Core.Loging;

using SMG.ServiceContract.Migration;
using SMG.ServiceContract.System;
using SMG.ServiceContract.Program;
using SMG.Application.DTO.Migration;
using SMG.Application.DTO.System;
using SMG.Application.DTO.Program;
using SMG.Monitor.Domain.Models;
using SMG.Monitor.Domain.Processor;


namespace SMG.Monitor.Domain.Services
{
    /// <summary>
    /// 二级任务监视调度器
    /// </summary>
    public class SecondTaskDispatcher : DispatcherBase
    {
        /// <summary>
        /// 节目单迁移服务
        /// </summary>
        IPlayMoveService playMoveService = ServiceLocator.Instance.Resolve<IPlayMoveService>();
        //日志服务
        ILogger logServer = LogService.GetInstance(LogType.Text);

        /// <summary>
        /// 每次取数据条数
        /// </summary>
        readonly Int32 count = 10;

        /// <summary>
        /// 调度配置
        /// </summary>
        DispatcherSetting dispatcherSetting;

        public SecondTaskDispatcher()
        {
            dispatcherSetting = new DispatcherSetting();
        }

        #region DispatcherBase Members

        /// <summary>
        /// 间隔
        /// </summary>
        protected override long Interval
        {
            get { return dispatcherSetting.DispatcherInterval; }
        }
        /// <summary>
        /// 是否忽略一场
        /// </summary>
        protected override bool IsIgnoreException
        {
            get { return true; }
        }
        /// <summary>
        /// 是否立即启动
        /// </summary>
        protected override bool IsStartImmediate
        {
            get { return true; }
        }

        /// <summary>
        /// 调度方法
        /// </summary>
        public override void Dispatching()
        {
            var plays = playMoveService.GetSecondMoveTask(count);
            while (plays != null && plays.Any()) {
                try
                {
                    foreach (var play in plays)
                    {
                        IProcessor processor = null;
                        if (play.PlayType == 0)
                        {
                            processor = new PlayProcessor();
                        }
                        else if (play.PlayType == 1)
                        {
                            processor = new TemplateProcessor();
                        }
                        processor.Preprocess(play);
                    }

                    plays = playMoveService.GetFirstMoveTask(count);
                }
                catch (Exception ex)
                {
                    logServer.Info("FirstTaskDispatcher.Dispatching.Exception", ex.Message);
                }
            }
        }

        #endregion

    }
}
